﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Zhongy
{
    public class AccountHandler:IHandler
    {
        private AccountCache accountCache = Cache.Account;
        public override void OnDisconnect(ClientPer client)
        {
            if (accountCache.IsOnline(client))
                accountCache.Offline(client);
        }

        public override void OnReceive(ClientPer client, SocketReceiveMsg msg)
        {
            switch (msg.MT)
            {
                case OpCode.TYPE_TV_CHECK_CLIENT:
                    Login(client, msg);
                    break;

                case OpCode.TYPE_CLIENT_DISCONNECT:
                    ClientDisconnect2Game(client, msg);
                    break;

                default:
                    Debug.LogError("信息归属不对");
                    break;
            }
        }

        private void Login(ClientPer client,SocketReceiveMsg msg)
        {
            SingleExecute.Instance.Execute(() =>
            {
                bool versonVaild = EncodeTool.JudgeVersionValid(Config.protocolVersion, msg.Version);

                string status = !accountCache.IsOnline(client) ? "OK" : "ERROR";
                status = versonVaild ? "OK" : "DENIED";
                List<SendOption> sendOptions = new List<SendOption>();
                if (Config.DefaultSendOptionTime != null && Config.DefaultSendOptionTime.Count > 0)
                {
                    sendOptions.Add(new SendOption
                    {
                        name = "time",
                        type = (int)Config.DefaultSendOptionType.INT,
                        values = Config.DefaultSendOptionTime,
                    });
                }

                if (Config.DefaultSendOptionDifficulty != null && Config.DefaultSendOptionDifficulty.Count > 0)
                {
                    sendOptions.Add(new SendOption
                    {
                        name = "difficulty",
                        type = (int)Config.DefaultSendOptionType.INT,
                        values = Config.DefaultSendOptionDifficulty,
                    });
                }
                //Debug.LogError(sendOptions.Count);

                SocketSendMsg sendMsg = new SocketSendMsg
                {
                    MT = OpCode.TYPE_ACK2C,
                    ackid = msg.mesgid,
                    result = status,
                    mesg = string.Empty,
                    session = client.GetHashCode().ToString(),
                    options =  sendOptions,
                    feature = new SendFeature
                    {
                        pause = Config.SupportPause
                    }
                };


                if (status.Equals("OK", StringComparison.CurrentCultureIgnoreCase))
                {
                    Dispatch(TransCode.Net, NetEvtCode.Client2TcpMsg, new object[] {client, sendMsg});

                    Dispatch(TransCode.Net, GameEvtCode.GameConnect);
                    Dispatch(TransCode.Game, GameEvtCode.GameConnect);
                }
            });

        }

        private void ClientDisconnect2Game(ClientPer client,SocketReceiveMsg msg)
        {
            SingleExecute.Instance.Execute(() =>
            {
                string status = accountCache.IsOnline(client) ? "OK" : "ERROR";
                SocketSendMsg sendMsg = new SocketSendMsg
                {
                    MT = OpCode.TYPE_ACK2C,
                    ackid = msg.mesgid,
                    result = status,
                    mesg = string.Empty
                };


                Dispatch(TransCode.Net,NetEvtCode.Client2TcpMsg,new object[]{client,sendMsg});


                Dispatch(TransCode.Net, GameEvtCode.GameDisconnect);
                Dispatch(TransCode.Game, GameEvtCode.GameDisconnect);

            });

        }


    }
}
